
#include <QFile>
#include <QTextStream>
#include <QStyle>
#include <QDialog>
#include <QStyleOption>
#include "FramelessWidget.h"
/*********************************************************/
char g_skinQssFile[CharMaxLen] = ":/blue.qss";

void setSkin(QWidget *widget)
{
    if(widget)
    {
        QFile file(g_skinQssFile);
        if(file.open(QFile::ReadOnly | QFile::Text))
        {
            QTextStream stream(&file);
            widget->setStyleSheet(stream.readAll());
            file.close();
        }
    }
}
/*********************************************************/

FramelessWidget::FramelessWidget(QWidget* parent) : QWidget(parent)
{
    setMinimumSize(300,200);

    setWindowFlags( Qt::FramelessWindowHint );
    setAttribute(Qt::WA_TranslucentBackground);

    fh = new FramelessHelper;
    fh->setWidgetMovable(true);
    fh->activateOn(this);

    layout = new QVBoxLayout;
    layout->setContentsMargins(0,0,0,0);
    layout->setSpacing(0);
    layout->setMargin(WidgetShadoWidth);

    setLayout(layout);


}
FramelessWidget::~FramelessWidget()
{
    delete fh;
}
void FramelessWidget::drawShadow(QPainter &painter)
{
    //绘制左上角、左下角、右上角、右下角、上、下、左、右边框
    QList<QPixmap> pixmaps;
    pixmaps.append(QPixmap(":pic/shadow/shadow_left.png"));
    pixmaps.append(QPixmap(":pic/shadow/shadow_right.png"));
    pixmaps.append(QPixmap(":pic/shadow/shadow_top.png"));
    pixmaps.append(QPixmap(":pic/shadow/shadow_bottom.png"));
    pixmaps.append(QPixmap(":pic/shadow/shadow_left_top.png"));
    pixmaps.append(QPixmap(":pic/shadow/shadow_right_top.png"));
    pixmaps.append(QPixmap(":pic/shadow/shadow_left_bottom.png"));
    pixmaps.append(QPixmap(":pic/shadow/shadow_right_bottom.png"));

    painter.drawPixmap(0, 0, WidgetShadoWidth, WidgetShadoWidth, pixmaps[4]);
    painter.drawPixmap(this->width()-WidgetShadoWidth, 0, WidgetShadoWidth, WidgetShadoWidth, pixmaps[5]);
    painter.drawPixmap(0,this->height()-WidgetShadoWidth, WidgetShadoWidth, WidgetShadoWidth, pixmaps[6]);
    painter.drawPixmap(this->width()-WidgetShadoWidth, this->height()-WidgetShadoWidth, WidgetShadoWidth, WidgetShadoWidth, pixmaps[7]);
    painter.drawPixmap(0, WidgetShadoWidth, WidgetShadoWidth, this->height()-2*WidgetShadoWidth, pixmaps[0].scaled(WidgetShadoWidth, this->height()-2*WidgetShadoWidth));
    painter.drawPixmap(this->width()-WidgetShadoWidth, WidgetShadoWidth, WidgetShadoWidth, this->height()-2*WidgetShadoWidth, pixmaps[1].scaled(WidgetShadoWidth, this->height()- 2*WidgetShadoWidth));
    painter.drawPixmap(WidgetShadoWidth, 0, this->width()-2*WidgetShadoWidth, WidgetShadoWidth, pixmaps[2].scaled(this->width()-2*WidgetShadoWidth, WidgetShadoWidth));
    painter.drawPixmap(WidgetShadoWidth, this->height()-WidgetShadoWidth, this->width()-2*WidgetShadoWidth, WidgetShadoWidth, pixmaps[3].scaled(this->width()-2*WidgetShadoWidth, WidgetShadoWidth));
}

/*********************************************************/

FramelessDialog::FramelessDialog(QWidget* parent):FramelessWidget(parent)
{
    titleBar = new MainWindowTitleBar(TitleBarElementType::ICON |
                                   TitleBarElementType::TITLE |
                                   TitleBarElementType::CLOSE);

    addWidget(titleBar, 0, Qt::AlignTop);
    setWidgetResizable(false);
    setCentered();
}


FramelessDialog::~FramelessDialog()
{
}


/*********************************************************/

MessageDialog::MessageDialog(const QString &title, MSGType _type, const QString &_message,
                             QWidget* parent) : QDialog(parent)
{
    initDialog(title);
    setType(_type, _message);
    initButtons();
}



void MessageDialog::initDialog(const QString& title)
{
    setFixedSize(300,200);

    setWindowFlags( Qt::FramelessWindowHint );
    setAttribute(Qt::WA_TranslucentBackground);

    setSkin(this);
    fh = new FramelessHelper;

    fh->setWidgetResizable(false);
    fh->setWidgetMovable(false);
    fh->activateOn(this);

    layout = new QVBoxLayout;
    layout->setMargin(WidgetShadoWidth);
    layout->setSpacing(5);
    setLayout(layout);

    titleBar = new MainWindowTitleBar(TitleBarElementType::ICON |
                            TitleBarElementType::TITLE );

    titleBar->setElement(TitleBarElementType::TITLE, title);
    layout->addWidget(titleBar, 0, Qt::AlignTop);

    setCentered();


}

void MessageDialog::setType(MSGType _type, const QString &_message)
{

    QHBoxLayout* lay = new QHBoxLayout;
    lay->setSpacing(5);
    lay->setContentsMargins(30, 30, 20, 30);
    layout->addLayout(lay);

    QLabel* icon = new QLabel;
    icon->setFixedSize(48, 48);
    lay->addWidget(icon);

    QLabel* message = new QLabel(_message);
    lay->addWidget(message);

    switch (_type) {
    case INFORMATION:
    {
        icon->setPixmap(QPixmap(":/pic/message/information.png"));
    }
        break;
    case WARNING:
    {
        icon->setPixmap(QPixmap(":/pic/message/alert.png"));
    }
        break;
    case CRITICAL:
    {
        icon->setPixmap(QPixmap(":/pic/message/delete.png"));
    }
        break;
    default:
        break;
    }
}


void MessageDialog::initButtons()
{
    acceptButton = new QPushButton(QIcon(QPixmap(":/pic/comfirm.png")), "确定");
    rejectButton  = new QPushButton(QIcon(QPixmap(":/pic/cancel.png")), "取消");

    QHBoxLayout* layButton = new QHBoxLayout;
    layButton->setMargin(10);
    layButton->addWidget(acceptButton, 0, Qt::AlignRight);
    layButton->addWidget(rejectButton, 0, Qt::AlignRight);
    layout->addLayout(layButton);
    connect(acceptButton, &QPushButton::clicked, this,
            [=]()
            {
                accept();
            });
    connect(rejectButton, &QPushButton::clicked, this,
            [=]()
            {
                reject();
            });
}



void MessageDialog::drawShadow(QPainter &painter)
{
    //绘制左上角、左下角、右上角、右下角、上、下、左、右边框
    QList<QPixmap> pixmaps;
    pixmaps.append(QPixmap(":pic/shadow/shadow_left.png"));
    pixmaps.append(QPixmap(":pic/shadow/shadow_right.png"));
    pixmaps.append(QPixmap(":pic/shadow/shadow_top.png"));
    pixmaps.append(QPixmap(":pic/shadow/shadow_bottom.png"));
    pixmaps.append(QPixmap(":pic/shadow/shadow_left_top.png"));
    pixmaps.append(QPixmap(":pic/shadow/shadow_right_top.png"));
    pixmaps.append(QPixmap(":pic/shadow/shadow_left_bottom.png"));
    pixmaps.append(QPixmap(":pic/shadow/shadow_right_bottom.png"));

    painter.drawPixmap(0, 0, WidgetShadoWidth, WidgetShadoWidth, pixmaps[4]);
    painter.drawPixmap(this->width()-WidgetShadoWidth, 0, WidgetShadoWidth, WidgetShadoWidth, pixmaps[5]);
    painter.drawPixmap(0,this->height()-WidgetShadoWidth, WidgetShadoWidth, WidgetShadoWidth, pixmaps[6]);
    painter.drawPixmap(this->width()-WidgetShadoWidth, this->height()-WidgetShadoWidth, WidgetShadoWidth, WidgetShadoWidth, pixmaps[7]);
    painter.drawPixmap(0, WidgetShadoWidth, WidgetShadoWidth, this->height()-2*WidgetShadoWidth, pixmaps[0].scaled(WidgetShadoWidth, this->height()-2*WidgetShadoWidth));
    painter.drawPixmap(this->width()-WidgetShadoWidth, WidgetShadoWidth, WidgetShadoWidth, this->height()-2*WidgetShadoWidth, pixmaps[1].scaled(WidgetShadoWidth, this->height()- 2*WidgetShadoWidth));
    painter.drawPixmap(WidgetShadoWidth, 0, this->width()-2*WidgetShadoWidth, WidgetShadoWidth, pixmaps[2].scaled(this->width()-2*WidgetShadoWidth, WidgetShadoWidth));
    painter.drawPixmap(WidgetShadoWidth, this->height()-WidgetShadoWidth, this->width()-2*WidgetShadoWidth, WidgetShadoWidth, pixmaps[3].scaled(this->width()-2*WidgetShadoWidth, WidgetShadoWidth));
}

MessageDialog::~MessageDialog()
{
}
void MessageDialog::setButtonVisualable(DialogButton bt, bool b)
{
    switch (bt) {
    case ACCEPT:
    {
        acceptButton->setVisible(b);
        break;
    }
    case REJECT:
    {
        rejectButton->setVisible(b);
        break;
    }
    default:
        break;
    }
}

/*********************************************************/


FramelessMainWindow::FramelessMainWindow()
{
    setMinimumSize(300,200);

    setWindowFlags( Qt::FramelessWindowHint );
    setAttribute(Qt::WA_TranslucentBackground);

    //fh = new NcFramelessHelper;
    fh = new FramelessHelper;
    fh->activateOn(this);
    fh->setWidgetMovable(false);

    layout = new QVBoxLayout;
    QWidget* w = new QWidget;
    w->setAttribute(Qt::WA_TranslucentBackground);
    w->setLayout(layout);
    layout->setSpacing(0);
    layout->setMargin(WidgetShadoWidth);

    setCentralWidget(w);

}
FramelessMainWindow::~FramelessMainWindow()
{
    delete fh;
}

void FramelessMainWindow::drawShadow(QPainter &painter)
{
    //绘制左上角、左下角、右上角、右下角、上、下、左、右边框
    QList<QPixmap> pixmaps;
    pixmaps.append(QPixmap(":pic/shadow/shadow_left.png"));
    pixmaps.append(QPixmap(":pic/shadow/shadow_right.png"));
    pixmaps.append(QPixmap(":pic/shadow/shadow_top.png"));
    pixmaps.append(QPixmap(":pic/shadow/shadow_bottom.png"));
    pixmaps.append(QPixmap(":pic/shadow/shadow_left_top.png"));
    pixmaps.append(QPixmap(":pic/shadow/shadow_right_top.png"));
    pixmaps.append(QPixmap(":pic/shadow/shadow_left_bottom.png"));
    pixmaps.append(QPixmap(":pic/shadow/shadow_right_bottom.png"));

    painter.drawPixmap(0, 0, WidgetShadoWidth, WidgetShadoWidth, pixmaps[4]);
    painter.drawPixmap(this->width()-WidgetShadoWidth, 0, WidgetShadoWidth, WidgetShadoWidth, pixmaps[5]);
    painter.drawPixmap(0,this->height()-WidgetShadoWidth, WidgetShadoWidth, WidgetShadoWidth, pixmaps[6]);
    painter.drawPixmap(this->width()-WidgetShadoWidth, this->height()-WidgetShadoWidth, WidgetShadoWidth, WidgetShadoWidth, pixmaps[7]);
    painter.drawPixmap(0, WidgetShadoWidth, WidgetShadoWidth, this->height()-2*WidgetShadoWidth, pixmaps[0].scaled(WidgetShadoWidth, this->height()-2*WidgetShadoWidth));
    painter.drawPixmap(this->width()-WidgetShadoWidth, WidgetShadoWidth, WidgetShadoWidth, this->height()-2*WidgetShadoWidth, pixmaps[1].scaled(WidgetShadoWidth, this->height()- 2*WidgetShadoWidth));
    painter.drawPixmap(WidgetShadoWidth, 0, this->width()-2*WidgetShadoWidth, WidgetShadoWidth, pixmaps[2].scaled(this->width()-2*WidgetShadoWidth, WidgetShadoWidth));
    painter.drawPixmap(WidgetShadoWidth, this->height()-WidgetShadoWidth, this->width()-2*WidgetShadoWidth, WidgetShadoWidth, pixmaps[3].scaled(this->width()-2*WidgetShadoWidth, WidgetShadoWidth));
}






